import Vue from 'vue';
import Vuex from 'vuex';
import { Environment } from '@/environments/environment';

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    codelists: [],
    appdata: '',
    localdata: {},
    indexselected:'',
  },
  getters: {

    /**
     * 获取代码表对象
     * 
     * @param state 
     */
    getCodeList:(state: any) => (srfkey: string) => {
      return state.codelists.find((_codelist: any) => Object.is(_codelist.srfkey, srfkey));
    },
    
    /**
     * 获取代码表
     * 
     * @param state 
     */
    getCodeListItems:(state: any) => (srfkey: string) => {
      let items: any[] = [];
      const codelist = state.codelists.find((_codelist: any) => Object.is(_codelist.srfkey, srfkey));
      if (!codelist) {
          console.log(`----${srfkey}----代码表不存在`);
      } else {
          items = [...codelist.items];
      }
      return items;
    },

    /**
     * 获取应用数据
     * 
     * @param state 
     */
    getAppData:(state: any) => () => {
      return state.appdata;
    },

    /**
     * 获取本地应用数据
     * 
     * @param state 
     */
    getLocalData:(state: any) => () => {
      return state.localdata;
    },

    /**
     * 获取首页选中数据
     * 
     * @param state 
     */
    getIndexSelected:(state:any) =>() =>{
      return state.indexselected;
    }

  },
  mutations: {

    /**
     * 添加代码表
     * 
     * @param state 
     * @param codelists 
     */
    addCodeLists(state: any, codelists: any) {
      state.codelists = [];
      state.codelists = [...codelists];
    },

    /**
     * 添加应用数据
     * 
     * @param state 
     * @param localdata 
     */
    addAppData(state: any, appdata: string) {
      state.appdata = appdata;
    },

    /**
    * 添加本地应用数据
    * 
    * @param state 
    * @param localdata 
    */
    addLocalData(state: any, localdata: any = {}) {
      state.localdata = {};
      Object.assign(state.localdata, localdata);
    },

    /**
    * 添加首页选中数据
    * 
    * @param state 
    * @param localdata 
    */
    setIndexSelected(state: any, selecteddata:string){
      state.indexselected = selecteddata;
    }
  },
  actions: {

  }
})
